<?php
namespace App\Http\Controllers\Admin\Platform;

use App\Service\Admin\Platform\AccountService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class AccountController extends BaseController
{
    //账户

    public function __construct(AccountService $service)
    {
        $this->db = $service;
    }

    //个人列表
    public function getUserIndex(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['uname'] = $request->post('realname', ''); //搜索
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getUserIndex($pas, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //企业列表
    public function getCompanyIndex(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['uname'] = $request->post('realname', ''); //搜索
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getCompanyIndex($pas, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //代理商列表
    public function getAgentIndex(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['uname'] = $request->post('realname', ''); //搜索
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getAgentIndex($pas, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //选项
    public function getOption()
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //上传图片
    public function upload(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $resUpload = Upload::uploadImg($request, 'img');
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        $fpath = $resUpload['data'];
        $res = $this->db->upload($uid, $fpath);
        if (!$res) {
            return $this->toJson(-2, '保存失败');
        }
        $resData = [
            'img' => env('DOMAIN') . '/' . $fpath,
        ];
        return $this->toJson(0, '', $resData);
    }

    //账户添加
    public function addUser(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['realname'] = $request->post('realname', ''); //姓名
        $pas['phone'] = $request->post('phone', ''); //手机号
        $pas['uname'] = $request->post('uname', ''); //账户名称
        $pas['passwd'] = $request->post('passwd', '123456'); //密码
        $pas['role_type'] = (int) $request->post('role_type', 0); //用户角色
        $gs['cname'] = $request->post('cname', ''); //公司名称
        $pas['end_time'] = $request->post('end_time', ''); //到期时间
        $pas['lang_front'] = $request->post('lang_front', 0); //前端语言
        $pas['lang_back'] = $request->post('lang_back', 0); //后端语言
        $pas['db_type'] = $request->post('db_type', 0); //数据库类型
        $pas['passwd'] = $pas['passwd'] ? $pas['passwd'] : '123456';
        $pas['lang_front'] = $pas['lang_front'] ? $pas['lang_front'] : 0;
        $pas['lang_back'] = $pas['lang_back'] ? $pas['lang_back'] : 0;
        $pas['db_type'] = $pas['db_type'] ? $pas['db_type'] : 0;
        if (mb_strlen($pas['realname']) < 2) {
            return $this->toJson(-2, '姓名不能少于2个字符');
        }
        if (mb_strlen($pas['uname']) < 2) {
            return $this->toJson(-2, '账户名不能少于2个字符');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $pas['phone'])) {
            return $this->toJson(-2, '手机号格式错误');
        }
        if (strlen($pas['passwd'])<6 || strlen($pas['passwd'])>20) {
            return $this->toJson(-2, '密码必须在6-20位之间');
        }
        if ($pas['role_type'] != 1) {
            return $this->toJson(-2, '用户角色错误');
        }
        if (!$pas['end_time']) {
            return $this->toJson(-2, '到期时间必须的');
        }
        if ($pas['end_time'] < toDate()) {
            return $this->toJson(-2, '到期时间不能早于当前时间');
        }
        $pas['by_uid'] = $uid;
        $res = $this->db->addUser($pas, $gs);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //用户修改
    public function modifyUser(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id');
        $pas['realname'] = $request->post('realname', ''); //姓名
        $pas['phone'] = $request->post('phone', ''); //手机号
        $pas['uname'] = $request->post('uname', ''); //账户名称
        $pas['passwd'] = $request->post('passwd', '123456'); //密码
        $pas['role_type'] = (int) $request->post('role_type', 0); //用户角色
        $gs['cname'] = $request->post('cname', ''); //公司名称
        $pas['end_time'] = $request->post('end_time', ''); //到期时间
        $pas['lang_front'] = $request->post('lang_front', 0); //前端语言
        $pas['lang_back'] = $request->post('lang_back', 0); //后端语言
        $pas['db_type'] = $request->post('db_type', 0); //数据库类型
        $pas['passwd'] = $pas['passwd'] ? $pas['passwd'] : '123456';
        $pas['lang_front'] = $pas['lang_front'] ? $pas['lang_front'] : 0;
        $pas['lang_back'] = $pas['lang_back'] ? $pas['lang_back'] : 0;
        $pas['db_type'] = $pas['db_type'] ? $pas['db_type'] : 0;
        if (mb_strlen($pas['realname']) < 2) {
            return $this->toJson(-2, '姓名不能少于2个字符');
        }
        if (mb_strlen($pas['uname']) < 2) {
            return $this->toJson(-2, '账户名不能少于2个字符');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $pas['phone'])) {
            return $this->toJson(-2, '手机号格式错误');
        }
        if (strlen($pas['passwd'])<6 || strlen($pas['passwd'])>20) {
            return $this->toJson(-2, '密码必须在6-20位之间');
        }
        if ($pas['role_type'] != 1) {
            return $this->toJson(-2, '用户角色错误');
        }
        if (!$pas['end_time']) {
            return $this->toJson(-2, '到期时间必须的');
        }
        if ($pas['end_time'] < toDate()) {
            return $this->toJson(-2, '到期时间不能早于当前时间');
        }
        $pas['by_uid'] = $uid;
        $res = $this->db->modifyUser($id, $pas, $gs);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 代理商添加
     */
    public function addAgent(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['realname'] = $request->post('realname', ''); //姓名
        $pas['phone'] = $request->post('phone', ''); //手机号
        $pas['uname'] = $request->post('uname', ''); //账户名称
        $pas['passwd'] = $request->post('passwd', '123456'); //密码
        $pas['role_type'] = (int) $request->post('role_type', 0); //代理商角色
        $gs['cname'] = $request->post('cname', ''); //公司名称
        $pas['start_time'] = $request->post('start_time', ''); //开始时间
        $pas['end_time'] = $request->post('end_time', ''); //到期时间
        $pas['passwd'] = $pas['passwd'] ? $pas['passwd'] : '123456';
        if (mb_strlen($pas['realname']) < 2) {
            return $this->toJson(-2, '姓名不能少于2个字符');
        }
        if (mb_strlen($pas['uname']) < 2) {
            return $this->toJson(-2, '账户名不能少于2个字符');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $pas['phone'])) {
            return $this->toJson(-2, '手机号格式错误');
        }
        if (strlen($pas['passwd'])<6 || strlen($pas['passwd'])>20) {
            return $this->toJson(-2, '密码必须在6-20位之间');
        }
        if ($pas['role_type'] != 2) {
            return $this->toJson(-2, '代理商角色错误');
        }
        if (strlen($gs['cname']) < 2) {
            return $this->toJson(-2, '公司名称不能少于2个字符');
        }
        if (!$pas['end_time'] || !$pas['start_time']) {
            return $this->toJson(-2, '到期时间或开始时间必须的');
        }
        if ($pas['end_time'] < toDate()) {
            return $this->toJson(-2, '到期时间不能早于当前时间');
        }
        if ($pas['start_time'] > $pas['end_time']) {
            return $this->toJson(-2, '到期时间不能早于开始时间');
        }
        $pas['by_uid'] = $uid;
        $res = $this->db->addAgent($pas, $gs);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //代理商修改
    public function modifyAgent(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id');
        $pas['realname'] = $request->post('realname', ''); //姓名
        $pas['phone'] = $request->post('phone', ''); //手机号
        $pas['uname'] = $request->post('uname', ''); //账户名称
        $pas['passwd'] = $request->post('passwd', '123456'); //密码
        $pas['role_type'] = (int) $request->post('role_type', 0); //代理商角色
        $gs['cname'] = $request->post('cname', ''); //公司名称
        $pas['start_time'] = $request->post('start_time', ''); //开始时间
        $pas['end_time'] = $request->post('end_time', ''); //到期时间
        $pas['passwd'] = $pas['passwd'] ? $pas['passwd'] : '123456';
        if (mb_strlen($pas['realname']) < 2) {
            return $this->toJson(-2, '姓名不能少于2个字符');
        }
        if (mb_strlen($pas['uname']) < 2) {
            return $this->toJson(-2, '账户名不能少于2个字符');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $pas['phone'])) {
            return $this->toJson(-2, '手机号格式错误');
        }
        if (strlen($pas['passwd'])<6 || strlen($pas['passwd'])>20) {
            return $this->toJson(-2, '密码必须在6-20位之间');
        }
        if ($pas['role_type'] != 2) {
            return $this->toJson(-2, '代理商角色错误');
        }
        if (strlen($gs['cname']) < 2) {
            return $this->toJson(-2, '公司名称不能少于2个字符');
        }
        if (!$pas['end_time']) {
            return $this->toJson(-2, '到期时间必须的');
        }
        if ($pas['end_time'] < toDate()) {
            return $this->toJson(-2, '到期时间不能早于当前时间');
        }
        if ($pas['start_time'] > $pas['end_time']) {
            return $this->toJson(-2, '到期时间不能早于开始时间');
        }
        $pas['by_uid'] = $uid;
        $res = $this->db->modifyAgent($id, $pas, $gs);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //禁用启用
    public function setEnable(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $enable = $request->post('is_enable', 0); //1禁用，2启用
        if (!in_array($enable, [1, 2])) {
            return $this->toJson(-2, '状态值错误');
        }
        $res = $this->db->setEnable($id, $enable);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $ids = $request->post('ids', ''); //IDS，通过|拼接
        if (!$ids) {
            return $this->toJson(-2, '没有记录ID');
        }
        $ids = explode('|', $ids);
        $res = $this->db->setDel($ids);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}